package control;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.UserDao;
import Dao.UserDaoimpl;
import entity.User;
import service.UserService;
import service.UserServicsImpl;

public class Regist extends HttpServlet{
	UserService us =  new UserServicsImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		resp.setCharacterEncoding("Gbk");
		req.setCharacterEncoding("utf-8");
		System.out.println("regist post");
		String email = req.getParameter("email");
		String password = req.getParameter("password");
		String sex = req.getParameter("sex");
		String name = req.getParameter("name");
		String typeid = req.getParameter("typeid");
		System.out.println(name);
		User u = new User();
		u.setEmail(email);
		u.setPassword(password);
		u.setName(name);
		if(typeid == null) {
			u.setTypeid(0);
		}else {
			u.setTypeid(Integer.valueOf(typeid));
		}
		
		u.setSex(sex);
		Integer add = 0;
		try {
			add = us.register(u);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(add);
		if(add == -1){
			System.out.println("已注册");
			resp.getWriter().print("<script charset=\"utf-8\"> alert(\"账号名已被注册\");  window.history.back(-1); </script>");			
			return ;
		}else {
			System.out.println("未注册");
			resp.getWriter().print("<script> alert(\"注册成功\");  window.history.back(-1); /* window.location.href=\"login&regist.html\" */;</script>");
			return ;
		}
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
		System.out.println("get？？？？");
	}
	


}
